package com.hp.bon.sgw.wh.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
 * CopyRright (c)2016/12-:SGW_WH2.1_TMALL_20180102
 * Project:SGW_WH2.1_TMALL_20180102
 * Module ID:BillDetailQueryConfigService
 * Comments:TODO
 * @author :chenjun  
 * Create Date：2019年1月8日
 * Modified By：Chenjun                     
 * Modified Date:2019年1月8日
 * Why:TODO
 * Version:<v1.0> 
 */
@Path("/")
public class BillDetailQueryConfigService {


	private static final Logger logger = LoggerFactory.getLogger(BillDetailQueryConfigService.class);
	private static String billDetailCfgFile = "./etc/billDetail_resource_config.properties";

	private static volatile Configuration config;

	public void init() {
		this.loadCfgFile();
	}

	@GET
	@Path("billdetailqueryconfig/reload")
	@Produces(MediaType.APPLICATION_JSON)
	public String loadCfgFile() {
		logger.info("[BillDetailQueryConfigService] load properties file - " + billDetailCfgFile
				+ " start...");
		boolean isSuccess = true;
		File paramFile = new File(billDetailCfgFile);
		InputStreamReader in = null;
		PropertiesConfiguration propConfig = new PropertiesConfiguration();
		try {
			in = new InputStreamReader(new BufferedInputStream(
					new FileInputStream(paramFile)), "utf-8");
			propConfig.setDelimiterParsingDisabled(true);
			propConfig.load(in);
		} catch (Exception e) {
			logger.warn("load properties file - exception ignored", e);
			isSuccess = false;
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				logger.error("load properties file", e);
				e.printStackTrace();
			}
		}
		config = propConfig;

		logger.info("[BillDetailQueryConfigService] load properties file - " + billDetailCfgFile
				+ (isSuccess ? " SUCCESS" : " FAILED"));
		return isSuccess ? "SUCCESS" : "FAILED";
	}

	public static String getValue(String name, String defaultValue) {
		return config.getString(name, defaultValue);
	}

	public static int getIntValue(String name, int defaultValue) {
		return config.getInt(name, defaultValue);
	}

	public static double getDoubleValue(String name, double defaultValue) {
		return config.getDouble(name, defaultValue);
	}

	public static long getLongValue(String name, long defaultValue) {
		return config.getLong(name, defaultValue);
	}

	public static boolean getBooleanValue(String name, boolean defaultValue) {
		return config.getBoolean(name, defaultValue);
	}

}
